package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.flow.dto.FlowParamDto;
import com.aiti.lulian.flow.dto.FlowRejectParamDto;
import com.aiti.lulian.flow.service.FlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-09-04 14:51
 **/
@RestController
@RequestMapping("/new-card/flow")
@Slf4j
@Api(tags = "流程业务接口")
public class NewCardFlowController extends WebController {

    @Autowired
    private FlowService flowService;

    @PostMapping("/start")
    @ApiOperation("发起流程")
    public Message startFlow(@RequestBody FlowParamDto flowParam) {
        try {
            flowParam.setUserId(findUserId());
            String flowInstanceId = flowService.startFlow(flowParam);
            return Message.success(flowInstanceId);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("发起流程异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @PostMapping("/restart")
    @ApiOperation("重新发起流程")
    public Message restartFlow(@RequestBody FlowParamDto flowParam) {
        try {
            flowParam.setUserId(findUserId());
            flowService.restartFlow(flowParam);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("重新发起流程异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @PutMapping("/approve")
    @ApiOperation("审批通过")
    public Message approve(@RequestParam String flowInstanceId) {
        try {
            flowService.approve(flowInstanceId, findUserId());
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("审批通过异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @PostMapping("/reject")
    @ApiOperation("驳回")
    public Message reject(@RequestBody FlowRejectParamDto param) {
        try {
            param.setUserId(findUserId());
            flowService.reject(param);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("驳回异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

    @GetMapping("/query-process")
    @ApiOperation("查询流程审批过程")
    public Message queryProcess(@RequestParam String flowInstanceId) {
        try {
            return Message.success(flowService.queryFlowInstanceProcess(flowInstanceId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询流程审批过程异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("接口异常："+ e.getMessage());
        }
    }

}
